package com.yishun.springboot.service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.RequestChengAnJu.CAYEleInfo;
import com.RequestChengAnJu.CAYPrFace;
import com.RequestGongWuShu.envMonitoring;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.mapper.GwsCWRMapper;
import com.yishun.springboot.mapper.RecordNumMapper;
import com.yishun.springboot.pojo.LayminiBaseInfoReturnBeans;
import com.yishun.springboot.pojo.GwsTeamType;
import com.yishun.springboot.pojo.TspHaveDevCode;
import com.yishun.springboot.pojo.UploadGWSAddTeam;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 工务署Service未修改
 * 
 * @author XiaoHuang
 *
 */
@Service
@Slf4j
public class LayminiYZWService {

	JSONObject resultsJson = new JSONObject();
	CAYEleInfo cayEleInfo = new CAYEleInfo();
	LayminiBaseInfoReturnBeans laymini = new LayminiBaseInfoReturnBeans();
	String res = ""; // response结果集的res判断变量装载体

	/**
	 * 按项目查询监督工程信息
	 * 
	 * @param d
	 * @return
	 */
	public JSONObject GetGcbyProj(JSONObject jsonObject) {
		CAYPrFace c = new CAYPrFace();
		LayminiBaseInfoReturnBeans laymini = new LayminiBaseInfoReturnBeans();

		String a = JSON.toJSONString(jsonObject); // 转换前端类型
		String b = c.getGcbyProj(a); // 返回String类型结果
		JSONObject c2 = JSON.parseObject(b); // 转为map拿res封装进res属性
		Object data = c2.get("res"); // 拿res封装进res属性

		laymini.setCode(0);
		laymini.setMsg("");
		laymini.setCount(1000);
		laymini.setData(data);
		String string = JSON.toJSONString(laymini);
		JSONObject results = JSON.parseObject(string);
//				JSONObject results = JSON.parseObject(string);
		return results;

	}

	/**
	 * 分页模糊查询监督项目信息
	 * 
	 * @param d
	 * @return
	 */
	public JSONObject GetProjInfos(JSONObject jsonObject) {
		CAYPrFace c = new CAYPrFace();
		LayminiBaseInfoReturnBeans laymini = new LayminiBaseInfoReturnBeans();

		String a = JSON.toJSONString(jsonObject); // 转换前端类型
		String b = c.getProjInfos(a); // 返回String类型结果
		JSONObject c2 = JSON.parseObject(b); // 转为map拿res封装进res属性 c2就是返回转成对象后的结果集合
		Object data = c2.get("res"); // 拿res封装进res属性
		int total = (int) c2.get("total");

		laymini.setCode(0);
		laymini.setMsg("");
		laymini.setCount(total);
		laymini.setData(data);
		String string = JSON.toJSONString(laymini);
		JSONObject results = JSON.parseObject(string);
//				JSONObject results = JSON.parseObject(string);
		return results;

	}

	/**
	 * 上报升降机设备基本信息
	 * 
	 * @param d
	 * @return
	 */
	public JSONObject uploadEleInfo(JSONObject jsonObject) {

		String reqString = JSON.toJSONString(jsonObject); // 转换前端类型
		String resultsStr = cayEleInfo.uploadEleInfo(reqString); // 返回String类型结果

		try {
			resultsJson = JSON.parseObject(resultsStr); //
		} catch (Exception e) {
			log.info("e:" + e);
			return resultsJson;
		}
		try {
			res = resultsJson.getString("res");
			if (res.equals("FAIL") || res.equals("ERROR")) {
				return resultsJson;
			}

			resultsJson = setLayminiResults(resultsJson);

			return resultsJson;
		} catch (Exception e) {
			return resultsJson;
			// TODO: handle exception
		}

	}

	public JSONObject setLayminiResults(JSONObject resultsJson) {

		laymini.setCode((Integer) resultsJson.get("errcode"));
		laymini.setMsg((String) resultsJson.get("msg"));
		laymini.setCount(1);
		laymini.setData(resultsJson.get("msg"));
		JSONObject jsonObject = (JSONObject) JSON.toJSON(laymini);

		return jsonObject;
	}

}
